<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <button>获取users</button>

    <script>
        let btns = document.querySelectorAll('button');

        btns[0].onclick = function() {
            // request({
            //     method: 'get',
            //     url: '/users',
            //     callback(data) {
            //         console.log(data)
            //     }
            // });

            request({
                method: 'get',
                url: '/users',
            }).then(data => {
                console.log(data);
            });
        }


        function request(options) {
            return new Promise(((resolve, reject) => {
                let xhr = new XMLHttpRequest()

                xhr.open(options.method, options.url)

                xhr.onload = function() {
                    // options.callback(this.responseText);
                    // resolve(this.responseText);
                    resolve({
                        data: JSON.parse(this.responseText)
                    })
                }

                xhr.send(options.data);
            }))
        }
    </script>
</body>
</html>
